package com.example.yi.allWithYou.api;

import android.app.Activity;
import android.app.ProgressDialog;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by yi on 15/3/24.
 */
public class ApiClientUsage {

    public static JSONObject dataResponse;

    private static ProgressDialog mLoginProgressDialog;


    public static void userLogin( Activity activity,RequestParams params, final DataHandler dataHandler ) throws JSONException {


        if(mLoginProgressDialog == null) {
            mLoginProgressDialog = new ProgressDialog(activity);
            mLoginProgressDialog.setCancelable(true);
            mLoginProgressDialog.setCanceledOnTouchOutside(false);
            mLoginProgressDialog.setMessage("登录中");
        }
         ApiClient.get("users/login", params, new JsonHttpResponseHandler() {

            @Override
            public void onStart() {
                super.onStart();
                mLoginProgressDialog.show();
            }

            //返回的数据必须是一个json对象
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject responseString) {
                super.onSuccess(statusCode, headers, responseString);
                dataResponse = responseString;
                mLoginProgressDialog.hide();
                System.out.println("response2:  " + responseString);
                dataHandler.getData(responseString);
            }
            //当没有返回数据或者返回数据格式不对的时候都会调用这个方法
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                mLoginProgressDialog.hide();
                System.out.println("response3:  " + responseString);

            }
        });
    }
}
